home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pctchnqs / 1991 / number2 / vtdlg.pas < prev   
Pascal/Delphi Source File  |  1990-10-04  |  5KB  |  191 lines

  1. unit VTDlg;
  2. {$X+}
  3.  
  4. Interface
  5. uses ComIO, Drivers, App, Memory, Dialogs, Objects,
  6.   MsgBox, TextView, Views;
  7.  
  8. type
  9.   TLineSettings = record
  10.     Port, Baud, DataBits, Parity, StopBits: Word;
  11.   end;
  12.   PComWindow = ^TComWindow;
  13.   TComWindow = object(TWindow)
  14.     Terminal: PTerminal;
  15.     LineSettings: TLineSettings;
  16.     LocalEcho: Boolean;
  17.     Win: Text;
  18.     constructor Init;
  19.     destructor Done; virtual;
  20.     procedure HandleEvent(VAR Event: TEvent); virtual;
  21.     Function InitCOM(LineParam: TLineSettings): Boolean;
  22.     procedure SetLocalEcho;
  23.     procedure SetLineDialog;
  24.   end;
  25.  
  26. procedure ErrorDialog(S: String);
  27.  
  28. Implementation
  29.  
  30. procedure ErrorDialog(S: String);
  31. begin
  32.   MessageBox(S, nil, mfOkButton);
  33. end;
  34.  
  35. constructor TComWindow.Init;
  36. var R: TRect;
  37. begin
  38.   Desktop^.GetExtent(R);
  39.   R.Grow(1,1);
  40.   TWindow.Init(R, 'Turbo Vision Terminal', wnNoNumber);
  41.   Options := (Options xor ofTopSelect) or ofSelectable;
  42.   Flags := Flags xor wfClose;
  43.   GetExtent(R);
  44.   R.Grow(-1,-1);
  45.   Terminal:=New(PTerminal, Init(R,
  46.     StandardScrollBar(sbHorizontal + sbHandleKeyboard),
  47.     StandardScrollBar(sbVertical + sbHandleKeyboard), 8000));
  48.   Insert(Terminal);
  49.   AssignDevice(Win, Terminal);
  50.   ReWrite(Win);
  51.   with LineSettings do
  52.   begin
  53.     Port:=0;     { COM 1 }
  54.     Baud:=2;     { 2400 }
  55.     DataBits:=1; { 8 }
  56.     Parity:=0;   { No Parity }
  57.     StopBits:=0; { No Stops }
  58.   end;
  59.   ComInit(4000);
  60.   InitCOM(LineSettings);
  61.   LocalEcho:=False;
  62. end;
  63.  
  64. destructor TComWindow.Done;
  65. begin
  66.   ComDone;
  67.   TWindow.Done;
  68. end;
  69.  
  70. procedure TComWindow.HandleEvent(var Event: TEvent);
  71. begin
  72.   if (Event.What and evKeyDown) <> 0 then
  73.   begin
  74.     if Event.CharCode <> #0 then
  75.       case Event.CharCode of
  76.         #13: begin
  77.           ComPut(13);
  78.           if LocalEcho then Writeln(Win);
  79.           ClearEvent(Event);
  80.         end;
  81.         else begin
  82.           ComPut(Byte(Event.CharCode));
  83.           if LocalEcho then Write(Win, Event.CharCode);
  84.           ClearEvent(Event);
  85.         end;
  86.       end { CASE }
  87.     end;
  88.   TWindow.HandleEvent(Event);
  89. end;
  90.  
  91. Function TComWindow.InitCOM(LineParam: TLineSettings): Boolean;
  92. const
  93.   BaudArr: array[0..4] of Byte = (Baud300, Baud1200, Baud2400,
  94.     Baud4800, Baud9600);
  95.   DataBitsArr: array[0..1] of Byte = (WordSize7, WordSize8);
  96.   ParityArr: array[0..2] of Byte = (NoParity, EvenParity, OddParity);
  97.   StopBitsArr: array[0..1] of Byte = (StopBits1, StopBits2);
  98. begin
  99.   with LineParam do
  100.   if ComSetParam(Port,
  101.     BaudArr[Baud] or
  102.     DataBitsArr[DataBits] or
  103.     ParityArr[Parity] or
  104.     StopBitsArr[StopBits])
  105.   then InitCOM:=True
  106.   else begin
  107.     InitCOM:=False;
  108.     ErrorDialog('Cannot initialize COM'+char(Ord('1')+Port));
  109.   end;
  110. end;
  111.  
  112. procedure TComWindow.SetLocalEcho;
  113. begin
  114.   LocalEcho := MessageBox('Local echo needed? ', nil,
  115.     mfYesButton + mfNoButton) = cmYes;
  116. end;
  117.  
  118. procedure TComWindow.SetLineDialog;
  119. var
  120.   D: PDialog;
  121.   Control: PView;
  122.   R: TRect;
  123.   Help: TLineSettings;
  124. begin
  125.   R.Assign(0,0,37,18);
  126.   D := New(PDialog, Init(R, 'Line Settings'));
  127.   with D^ do
  128.   begin
  129.     Options := Options or ofCentered;
  130.     R.Assign(5, 3, 16, 5);
  131.     Control := New(PRadioButtons, Init(R,
  132.       NewSItem('COM ~1~',
  133.       NewSItem('COM ~2~', nil))));
  134.     Insert(Control);
  135.     R.Assign(4, 2, 11, 3);
  136.     Insert(New(PLabel, Init(R, '~P~ort', Control)));
  137.     R.Assign(5, 7, 16, 12);
  138.     Control := New(PRadioButtons, Init(R,
  139.       NewSItem('300',
  140.       NewSItem('1200',
  141.       NewSItem('2400',
  142.       NewSItem('4800',
  143.       NewSItem('9600', nil)))))));
  144.     Insert(Control);
  145.     R.Assign(4, 6, 11, 7);
  146.     Insert(New(PLabel, Init(R, '~B~aud', Control)));
  147.     R.Assign(22, 3, 32, 5);
  148.     Control := New(PRadioButtons, Init(R,
  149.       NewSItem('~7~',
  150.       NewSItem('~8~', nil))));
  151.     Insert(Control);
  152.     R.Assign(21, 2, 32, 3);
  153.     Insert(New(PLabel, Init(R, '~D~ata Bits', Control)));
  154.     R.Assign(22, 7, 32, 10);
  155.     Control := New(PRadioButtons, Init(R,
  156.       NewSItem('~N~one',
  157.       NewSItem('~E~ven',
  158.       NewSItem('~O~dd', nil)))));
  159.     Insert(Control);
  160.     R.Assign(21, 6, 32, 7);
  161.     Insert(New(PLabel, Init(R, 'P~a~rity', Control)));
  162.     R.Assign(22, 12, 32, 14);
  163.     Control := New(PRadioButtons, Init(R,
  164.       NewSItem('~1~',
  165.       NewSItem('~2~', nil))));
  166.     Insert(Control);
  167.     R.Assign(21, 11, 32, 12);
  168.     Insert(New(PLabel, Init(R, '~S~top Bits', Control)));
  169.     R.Assign(4, 15, 14, 17);
  170.     Insert(New(PButton, Init(R, 'O~k~', cmOK, bfDefault)));
  171.     R.Assign(23, 15, 33, 17);
  172.     Insert(New(PButton, Init(R, 'Cancel', cmCancel, bfNormal)));
  173.     SelectNext(False);
  174.   end;
  175.   if LowMemory then
  176.   begin
  177.     Dispose(D, Done);
  178.     ErrorDialog('Out of memory');
  179.     DoneMemory;
  180.   end
  181.   else begin
  182.     D^.SetData(LineSettings);
  183.     if DeskTop^.ExecView(D) = cmOK then
  184.     begin
  185.       D^.GetData(Help);
  186.       if InitCOM(Help) then LineSettings:=Help;
  187.     end;
  188.     Dispose(D, Done);
  189.   end;
  190. end;
  191. end.